package com.akeil.flash.dataprocessing;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.akeil.flash.pojo.Parameter;
import com.yang.serialport.utils.ByteUtils;

/**
 * 
 * 
* Copyright: Copyright (c) 2018 HIT akeil
* 
* @ClassName: DanguanCommandTransfer.java
* @Description: 这个类是用于单管馏程仪器使用
*
* @version: v1.0.0
* @author: snow
* @date: 2018年8月5日 下午12:33:20 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2018年8月5日     snow           v1.0.0               修改原因
 */
public class FlashC51CommandTransfer implements ICommandTransfer {
	Map<Byte,Byte> mapRecv = new HashMap<Byte,Byte>();
	Map<Byte,Byte> mapSend = new HashMap<Byte,Byte>();
	
	public FlashC51CommandTransfer(){
		init();
	}
	
	public void init(){
		for(int i=-128;i<127;i++){
			mapRecv.put(Byte.valueOf((byte)i), Byte.valueOf((byte)i));
		}
		
		for(int i=-128;i<127;i++){
			mapSend.put(Byte.valueOf((byte)i), Byte.valueOf((byte)i));
		}

	}
	
	@Override
	public byte transferRecv(byte input) {
		// TODO Auto-generated method stub
		Byte ret = mapRecv.get(Byte.valueOf(input));

		return ret.byteValue();
	}
	
	@Test
	public void test(){
		init();
		Object ret = mapRecv.get(0);
	}

	@Override
	public byte transferSend(byte input) {
		Byte ret = mapSend.get(Byte.valueOf(input));
		
		return ret.byteValue();
	}

	@Override
	public byte[] generateCommand(byte input) {
		// TODO Auto-generated method stub
		byte[] cmds = new byte[14];
		cmds[0] = (byte)0xaa;//帧头
		cmds[1] = (byte)0x55;//帧头
		cmds[2] = (byte)0x05;//len
		cmds[3] = (byte)transferSend(input);//指令
		cmds[4] = (byte)0x00;//f1
		cmds[5] = (byte)0x00;//f2
		cmds[6] = (byte)0x00;//f3
		cmds[7] = (byte)0x00;//f4
		cmds[8] = (byte)0x00;//crc 0
		cmds[9] = (byte)0x00;//crc 1
		cmds[10] = (byte)0xcc;//帧尾
		cmds[11] = (byte)0x33;//帧尾
		cmds[12] = (byte)0xc3;//帧尾
		cmds[13] = (byte)0x3c;//帧尾
		
		byte crc = GenerateCRC(cmds,2,7);//
		cmds[8] = crc;
		
		return cmds;
	}
	
	public byte[] generateCommand(byte input,int position) {
		// TODO Auto-generated method stub
		byte[] ints = ByteUtils.intToByte(position);
		
		byte[] cmds = new byte[14];
		cmds[0] = (byte)0xaa;//帧头
		cmds[1] = (byte)0x55;//帧头
		cmds[2] = (byte)0x05;//len
		cmds[3] = (byte)transferSend(input);//指令
		cmds[4] = (byte)ints[0];//f1
		cmds[5] = (byte)ints[1];//f2
		cmds[6] = (byte)ints[2];//f3
		cmds[7] = (byte)ints[3];//f4
		cmds[8] = (byte)0x00;//crc
		cmds[9] = (byte)0x00;//crc
		cmds[10] = (byte)0xcc;//帧尾
		cmds[11] = (byte)0x33;//帧尾
		cmds[12] = (byte)0xc3;//帧尾
		cmds[13] = (byte)0x3c;//帧尾
		
		byte crc = GenerateCRC(cmds,2,7);//
		cmds[8] = crc;
		
		return cmds;
	}

	@Override
	public byte[] generateParameter(byte input,Parameter parameter) {
		// TODO Auto-generated method stub
		
		byte[] cmds = {(byte)0xaa,(byte)0x55,(byte)0x00,(byte)0x00};
		cmds[3] = (byte)transferSend(input);//指令
		
		cmds[2] = (byte)(cmds.length - 3);
		
		byte crc = GenerateCRC(cmds,2,cmds.length - 1);
		byte[] end = {(byte)crc,(byte)0xcc,(byte)0x33,(byte)0xc3,(byte)0x3c};
		cmds = ByteUtils.addBytes(cmds, end);
		
		return cmds;
	}
	
	private short float2ushort(double input)
	{
		boolean neg = false;
		if (input < 0.0)
		{
			neg = true;
		}
		
		short	r = 0;
		byte	t;
		t = (byte)(((int)((((input < 0) ? (-input) : input) * 10) + 0.5 )) & 0xff);
		
		if (neg)
		{
			r |= 0x0101;
		}
		
		r |= t;
		
		return r;	
	}

	private byte GenerateCRC(byte[] bytes,int start,int end){
		byte out,data;//用于保存异或结果 
		out = 0x00; 

		for (int i = start;i <= end ; i++){
			data = bytes[i];
			out ^= data;
		}
		return out;
	}

	@Override
	public byte[] generateFunctionFitParameter(byte input, Parameter parameter) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public byte[] generateCoefficient(byte input, double[] coefficients) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public byte[] generateFloatValue(byte input, float value) {
		// TODO 自动生成的方法存根
		byte[] ints = ByteUtils.floatTobyteArray(value);
		
		byte[] cmds = new byte[14];
		cmds[0] = (byte)0xaa;//帧头
		cmds[1] = (byte)0x55;//帧头
		cmds[2] = (byte)0x05;//len
		cmds[3] = (byte)transferSend(input);//指令
		cmds[4] = (byte)ints[0];//f1
		cmds[5] = (byte)ints[1];//f2
		cmds[6] = (byte)ints[2];//f3
		cmds[7] = (byte)ints[3];//f4
		cmds[8] = (byte)0x00;//crc
		cmds[9] = (byte)0x00;//crc
		cmds[10] = (byte)0xcc;//帧尾
		cmds[11] = (byte)0x33;//帧尾
		cmds[12] = (byte)0xc3;//帧尾
		cmds[13] = (byte)0x3c;//帧尾
		
		byte crc = GenerateCRC(cmds,2,7);//
		cmds[8] = crc;
		
		return cmds;
		
	}

}
